import { useUserStore } from '@/utils/stores/modules/user'


/**
 * 权限指令
 * @param el 元素
 * @param binding 指令参数
 * 使用方式：
 * <template>
 *     <div v-perms="['auth.menu/edit']">
 *         <a-button>编辑</a-button>
 *     </div>
 * </template>
 */
export default {
    mounted: (el: HTMLElement, binding: any) => {
        const { value } = binding
        const userStore = useUserStore()
        const permissions = userStore.permissions
        const all_permission = '*'
        if (Array.isArray(value)) {
            if (value.length > 0) {
                const hasPermission = permissions.some((key: string) => {
                    console.log(key)
                    return all_permission == key || value.includes(key)
                })

                if (!hasPermission) {
                    el.parentNode && el.parentNode.removeChild(el)
                }
            }
        } else {
            throw new Error('like v-perms="[\'auth.menu/edit\']"')
        }
    }
}
